﻿@using Seal.Model;
@{
    SecurityProvider provider = Model;

    //Parameters for this provider
    provider.Parameters.Add(new SecurityParameter() { Name = "ldap_server", DisplayName = "LDAP Server Name", Description = "The server name or address of the LDAP server" });
    provider.Parameters.Add(new SecurityParameter() { Name = "ldap_port", DisplayName = "LDAP Port", NumericValue = 389, Description = "The LDAP server port number" });
    provider.Parameters.Add(new SecurityParameter() { Name = "ldap_use_ssl", DisplayName = "Use secure socket layer", BoolValue = true, Description = "If true, the secure socket layer (SSL) are used" });
    provider.Parameters.Add(new SecurityParameter() { Name = "ldap_auth_type",DisplayName = "Authorization Type", Value = "Basic", Enums = new string[] { "Anonymous", "Basic", "Digest", "Dpa", "External", "Kerberos", "Msn", "Negotiate", "Ntlm", "Sicily" }, Description = "Specify the authentication method to use on a connection" });

    provider.Script = @"@using System.Net;
@using System.DirectoryServices.Protocols;
@{
    SecurityUser user = Model;
	//LDAP Authentication: use the user name and password to authenticate the user and set his name and security groups...
	//user.WebUserName -> user name from the login screen
	//user.WebPassword -> password from the login screen
	//user.WebPrincipal -> may be used for integrated windows authentication

	user.Name = user.WebUserName; //Display name
	//user.PersonalFolderName -> may be set to defined the personal folder name

    var credential = new NetworkCredential(user.WebUserName, user.WebPassword);
    var ldapConnection = new LdapConnection(new LdapDirectoryIdentifier(user.Security.GetValue(""ldap_server""), user.Security.GetNumericValue(""ldap_port""), false, false));

    ldapConnection.Credential = credential;
    ldapConnection.AuthType = (AuthType) Enum.Parse(typeof(AuthType), user.Security.GetValue(""ldap_auth_type""));
    ldapConnection.SessionOptions.SecureSocketLayer = user.Security.GetBoolValue(""ldap_use_ssl"");
    ldapConnection.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback((con, cer) => true);
    ldapConnection.Bind();

    user.AddDefaultSecurityGroup();

    //User default culture, theme and logo can be also overwritten with
    //user.SetDefaultCulture(group.Culture);
    //user.SetDefaultLogoName(group.LogoName);  

    /*
    OR make an request to find a group (to be implemented)...
    var request = new SearchRequest(""name"", ""cn="", System.DirectoryServices.Protocols.SearchScope.Subtree);
    var response = ldapConnection.SendRequest(request) as SearchResponse;
    user.AddSecurityGroup(response.Entries[0].ToString());	
    */
}";
}
